package top.huntwolf.admin.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import top.huntwolf.admin.pojo.orderEntity.Order;
import top.huntwolf.admin.service.BookReserveService;
import top.huntwolf.admin.service.OrderService;
import top.huntwolf.admin.utils.RedisUtil;


@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    private BookReserveService reserveService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private OrderService orderService;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredKey = message.toString();
        String key = expiredKey + "-0";
        //更新过期订单，存入数据库中
        if (expiredKey.startsWith("order")){
            Order order = (Order) redisUtil.get(key);
            reserveService.incompleteOrderIncreaseStorage(order);
            orderService.generateIncomplete(expiredKey);
        }
    }

}
